﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Eventing
{
    public delegate void EventHandler<TEvent>(TEvent eventMessage);



    public class EventRegistration<TEvent> : IEventHandler
    {
        private EventHandler<TEvent> _typedHandler;
        private Guid _id;


        public Guid Id
        {
            get { return _id; }
        }

        public Type HandledEventType
        {
            get { return typeof(TEvent); }
        }


        public EventRegistration(EventHandler<TEvent> typedHandler)
        {
            _typedHandler = typedHandler;
            _id = Guid.NewGuid();
        }


        public void Handle(object message)
        {
            _typedHandler.Invoke((TEvent)message);
        }

        public bool Equals(IEventHandler other)
        {
            bool isEqual = false;

            try
            {
                if (other is EventRegistration<TEvent>)
                {
                    EventRegistration<TEvent> otherRegistration = other as EventRegistration<TEvent>;

                    isEqual = Guid.Equals(_id, otherRegistration.Id);
                }
            }
            catch { }

            return isEqual;
        }
    }
}
